<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Default Kohana controller. This controller should NOT be used in production.
 * It is for demonstration purposes only!
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Thread_Controller extends BaseController {
	 
	public function __construct()
	{
		parent::__construct();
	}
	public  function ThreadList($forumid = NULL)
	{
		$this->template->title = 'E-study Forum List';
		$this->ThreadView($forumid);
	}
	private function ThreadView($forumid =null)
	{
		$this->template->content = new View('ThreadList');
		//$threads = ORM::factory('thread')->where('thr_forid', $subjectid)->orderby('thr_id', 'asc')->find_all();
		$threads = ORM::factory('thread')->where('thr_forid',$forumid)->find_all();
		//$forum = ORM::factory('forum')->where('for_id', $subjectid)->orderby('for_id', 'asc')->find();
		$forum = ORM::factory('forum')->find($forumid);
		$this->template->content->threadTitle = $forum->for_name;
		$this->template->content->forumid = $forumid ;
		$this->template->content->threads = $threads;
		$this -> template -> content->newthread = null;
	}
	public function AddThread($forumid =null)
	{
		$this->ThreadView($forumid);
		$this->template->title = 'Add Thread';
		$form = new Forge(url::current(),'',"post");
		$form->input('title')->label('Post Name')->rules('required|length[5,32]')->style('width:200px;');
		$form->Textarea('Content')->label('Post Content')->rules('required|length[5,200]')->cols('80')->rows('5');
		$form->submit('submit');
		$this -> template -> content->newthread = $form->render();
		if ($form->validate())
		{
			$thread = new thread_Model();
			$thread->thr_forid = $forumid;
			$thread->thr_author = $this->user->username;
			$thread->thr_authorid = $this->user->id;
			$thread->thr_subject = $form->title->value;
			$thread->thr_postdate = date("Y-m-d H:i:s");
			$thread->thr_lastpostdate = date("Y-m-d H:i:s");
			$thread->thr_lastpost = $this->user->id;
			$thread->thr_lastposter = $this->user->username;
			$thread->save();
			$post = new post_Model();
			$post->pos_thrid = $thread->id;
			$post->pos_author = $this->user->username;
			$post->Author_id = $this->user->id;
			$post->pos_postdate  = date("Y-m-d H:i:s");
			$post->pos_subject = $form->title->value;
			$post->pos_ipfrom  = Ip::get_real_ip();
			$post->pos_content = $form->Content->value;
			$post->save();
			url::redirect('Thread/ThreadList/'.$forumid);
		}
	}

}